package com.itheima.manager;

import com.itheima.common.PageResult;
import com.itheima.domain.*;
import com.itheima.service.*;
import com.itheima.vo.PmsProductResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class PmsProductManager {

    @DubboReference
    private PmsMemberPriceService memberPriceService;


    @DubboReference
    private CmsPrefrenceAreaProductRelationService prefrenceAreaProductRelationService;

    @DubboReference
    private PmsProductAttributeValueService pmsProductAttributeValueService;

    @DubboReference
    private PmsProductFullReductionService pmsProductFullReductionService;

    @DubboReference
    private PmsProductLadderService pmsProductLadderService;

    @DubboReference
    private PmsSkuStockService skuStockService;

    @DubboReference
    private CmsSubjectProductRelationService subjectProductRelationService;

    @DubboReference
    private PmsProductCategoryService pmsProductCategoryService;



    @DubboReference
    PmsProductService pmsProductService;


    public PageResult list(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        return pmsProductService.pageByParent(brandId,keyword,pageNum,pageSize,productCategoryId,productSn,publishStatus,verifyStatus);
    }

    public PmsProduct selectById(Integer id) {
        PmsProduct product = pmsProductService.getById(id);
        return product;
    }

    public void deleteStatus(ArrayList<Integer> ids, Integer deleteStatus) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductService.getById(id);
            pmsProduct.setDeleteStatus(deleteStatus);
            pmsProductService.updateById(pmsProduct);
        }
    }

    public void newStatus(ArrayList<Integer> ids, Integer newStatus) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductService.getById(id);
            pmsProduct.setNewStatus(newStatus);
            pmsProductService.updateById(pmsProduct);
        }
    }

    public void publishStatus(ArrayList<Integer> ids, Integer publishStatus) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductService.getById(id);
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductService.updateById(pmsProduct);
        }
    }

    public void recommendStatus(ArrayList<Integer> ids, Integer recommendStatus) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductService.getById(id);
            pmsProduct.setRecommandStatus(recommendStatus);
            pmsProductService.updateById(pmsProduct);
        }
    }

    public void create(PmsProductResult pmsProductVo) {
        //1.将商品信息存入商品表,并获取商品id
        pmsProductService.save(pmsProductVo);
        Long id = pmsProductVo.getId();

        //2.取出会员价格信息,为产品id赋值,存入会员价格表
        List<PmsMemberPrice> pmsMemberPrices = pmsProductVo.getMemberPriceList();
        if (pmsMemberPrices != null && pmsMemberPrices.size() != 0) {
            for (PmsMemberPrice pmsMemberPrice : pmsMemberPrices) {
                pmsMemberPrice.setProductId(id);
            }
            memberPriceService.saveBatch(pmsMemberPrices);
        }


        //3.取出优选关系信息,存入优选关系表
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelations = pmsProductVo.getPrefrenceAreaProductRelationList();
        if (cmsPrefrenceAreaProductRelations != null && cmsPrefrenceAreaProductRelations.size() != 0) {
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : cmsPrefrenceAreaProductRelations) {
                cmsPrefrenceAreaProductRelation.setProductId(id);
            }
            prefrenceAreaProductRelationService.saveBatch(cmsPrefrenceAreaProductRelations);
        }


        //4.取出产品属性信息,存入属性值表(这是中间表)
        List<PmsProductAttributeValue> pmsProductAttributeValues = pmsProductVo.getProductAttributeValueList();
        if (pmsProductAttributeValues != null && pmsProductAttributeValues.size() != 0) {
            for (PmsProductAttributeValue pmsProductAttributeValue : pmsProductAttributeValues) {
                pmsProductAttributeValue.setProductId(id);
            }
            pmsProductAttributeValueService.saveBatch(pmsProductAttributeValues);
        }

        //5.取出产品满减信息,存入产品满减表
        List<PmsProductFullReduction> pmsProductFullReductions = pmsProductVo.getProductFullReductionList();
        if (pmsProductFullReductions != null && pmsProductFullReductions.size() != 0) {
            for (PmsProductFullReduction pmsProductFullReduction : pmsProductFullReductions) {
                pmsProductFullReduction.setProductId(id);
            }
            pmsProductFullReductionService.saveBatch(pmsProductFullReductions);
        }

        //6.产品阶梯价格表
        List<PmsProductLadder> pmsProductLadders = pmsProductVo.getProductLadderList();
        if (pmsProductLadders != null && pmsProductLadders.size() != 0) {
            for (PmsProductLadder pmsProductLadder : pmsProductLadders) {
                pmsProductLadder.setProductId(id);
            }
            pmsProductLadderService.saveBatch(pmsProductLadders);
        }

        //7.库存警告表(sku)
        List<PmsSkuStock> pmsSkuStocks = pmsProductVo.getSkuStockList();
        if (pmsSkuStocks != null && pmsSkuStocks.size() != 0) {
            for (PmsSkuStock pmsSkuStock : pmsSkuStocks) {
                pmsSkuStock.setProductId(id);
                if (pmsProductVo.getPromotionType() == 1) {
                    //获得促销价
                    BigDecimal promotionPrice = pmsProductVo.getPromotionPrice();
                    //获得原价
                    BigDecimal price = pmsProductVo.getPrice();
                    //计算获得减少的价格
                    BigDecimal multiply = price.multiply(promotionPrice);
                    //获得同种商品每个品类的售价
                    BigDecimal price1 = pmsSkuStock.getPrice();
                    //减去减免的价格获得每一品类的促销价
                    BigDecimal multiply1 = price1.multiply(multiply);
                    //设置每一品类的促销价
                    pmsSkuStock.setPromotionPrice(multiply1);
                }
            }
            skuStockService.saveBatch(pmsSkuStocks);
        }

        //8.专题和商品关系中间表储存
        List<CmsSubjectProductRelation> cmsSubjectProductRelations = pmsProductVo.getSubjectProductRelationList();
        if (cmsSubjectProductRelations != null && cmsSubjectProductRelations.size() != 0) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : cmsSubjectProductRelations) {
                cmsSubjectProductRelation.setProductId(id);
            }
            subjectProductRelationService.saveBatch(cmsSubjectProductRelations);
        }
    }
}
